<?php

namespace App\Http\Controllers\Api;

use App\Category;
use App\Kind;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cateData = [];
        $cateFilter = collect();
        $kindData = [];
        $kindFilter = collect();
        foreach(Category::all() as $cate){
            $kinds = Category::find($cate->id)->kind;
            foreach ($kinds as $kind) {
                $kindData= [
                    'value' => $kind->id,
                    'label' => $kind->name
                ];
                $kindFilter->push($kindData);
            };
            $cateData = [
                'id' => $cate->id,
                'value' => $cate->id,
                'label' => $cate->name,
                'children' => $kindFilter
            ];
            $cateFilter->push($cateData);
            $kindFilter = collect();
        }
        return $cateFilter;
    }

    public function getKindName($kind)
    {
        return Kind::find($kind)->name;
    }
}
